#include "UserInfo.h"
#include "../../model/proxy/UserProxy.h"
#include "../../net/NetController.h"
#include "../../net/NCDefines.h"
#include "../../utils/LocalString.h"
UserInfo::UserInfo(void)
	:mLevel(NULL),mName(NULL),mEnergy(NULL),mPower(NULL),mGold(NULL),mMoney(NULL)
{
}

UserInfo::~UserInfo(void)
{
	CC_SAFE_RELEASE(mLevel);
	CC_SAFE_RELEASE(mName);
	CC_SAFE_RELEASE(mEnergy);
	CC_SAFE_RELEASE(mPower);
	CC_SAFE_RELEASE(mGold);
	CC_SAFE_RELEASE(mMoney);
}

bool UserInfo::init()
{
	return true;
}
cocos2d::SEL_MenuHandler UserInfo::onResolveCCBCCMenuItemSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onLevelClick", UserInfo::onLevelClick);
	return NULL;
}

void UserInfo::onLevelClick( cocos2d::CCObject * pSender )
{
    NetController::getSingletonPtr()->getSelfInfo();
//	Framework::getSingletonPtr()->popup("MoreDetailDialog", NULL, 0, 200);
	Framework::getSingletonPtr()->popup("LevelUpDialog", NULL);
}

cocos2d::extension::SEL_CCControlHandler UserInfo::onResolveCCBCCControlSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	return NULL;
}

bool UserInfo::onAssignCCBMemberVariable( cocos2d::CCObject * pTarget, cocos2d::CCString * pMemberVariableName, cocos2d::CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLevel", CCLabelTTF *, this->mLevel);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mName", CCLabelTTF *, this->mName);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEnergy", CCLabelTTF *, this->mEnergy);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPower", CCLabelTTF *, this->mPower);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mGold", CCLabelTTF *, this->mGold);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mMoney", CCLabelTTF *, this->mMoney);
	return false;
}

void UserInfo::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
    mProgress = ProgressText::create("sprite/progress_02.png",gls("font_yahei"),14);
	mProgress->setFormatStr(gls("complete_pecent"));
	mProgress->setRect(0, 100);
	mProgress->setPecent(100);
	mProgress->setPosition(90, 30);
	mProgress->getText()->setPosition(ccp(212,166));
    mProgress->setFormatStr("%f");
	addChild(mProgress);

	/*mName->setAnchorPoint(ccp(0, 0.5));
	mName->setContentSize(CCSizeMake(50, 80));
	mName->setString("ddddddddddddddddddddddd");
	CCSize fontSize = CCSizeMake(280, 50);
	mName->setDimensions(fontSize);
	CCSize size = mName->getContentSize();*/
}

void UserInfo::onEnter()
{
	CCNode::onEnter();    
    CCArray* nameList = CCArray::create(ccs(kNCRegist),ccs(kNCDungeonExplore),ccs(kNCSellEquipage),ccs(kNCChallengePlayer),ccs(kNCGetSelfInfo),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(UserInfo::_onNotification));
	refresh();
}

void UserInfo::onExit()
{
    RemoveObserver(this);
	CCNode::onExit();
}

void UserInfo::refresh()
{
    UserProxy* up = UserProxy::getSingletonPtr();
    UserVO& userVO = up->userVO;
	mLevel->setString(fcs("lv%d",userVO.level));
	mName->setString(up->userVO.name.c_str());
	mEnergy->setString(fcs("%d/%d",up->userVO.energyCur, up->userVO.energyMax));
	mPower->setString(fcs("%d/%d",up->userVO.powerCur, up->userVO.powerMax));
	mGold->setString(fcs("%d",up->userVO.coin));
	mMoney->setString(fcs("%d",up->userVO.money));
    mProgress->setPecent(float(up->userVO.exp));
}

void UserInfo::_onNotification(CCObject* object)
{
    CCLog("UserInfo::_onNotification[%x]", this);
    
	NotificationObserver* notification = (NotificationObserver*)object;
    std::string name = std::string(notification->getName());
	if (name == kNCRegist || name == kNCDungeonExplore || name == kNCSellEquipage ||
        name == kNCChallengePlayer || name == kNCGetSelfInfo)
	{
        refresh();
	}
}
